
************************************************************************************************
* Purpose: merge sample with group data to get group data at individual child level and coverage rates
* Using NOVAX group module
************************************************************************************************

do "D:\Data\workdata\708296\Project - Mother groups and mental health\Do\global.do" 

********************************************************************************
* Prepare file. Restrict to mother groups only, drop duplicates etc. 
********************************************************************************
use "$raw\gruppeb", clear
append using "$raw\gruppea"
append using "$raw\gruppec"
gen help=substr(cpr, -12,1) // few missing cpr spells in raw data
drop if help==""
drop help
duplicates drop
duplicates report cpr GRUPPEID1 GRUPPEID2 // no duplicates 
gen pnr = cpr 
gen cprm = cpr
drop cpr  

// Identify mother groups from Gruppetype (group type)
tab Gruppetype
gen mødregruppe = 0
replace mødregruppe = 1 if Gruppetype == "Mor/barn gruppe"
replace mødregruppe = 1 if Gruppetype == "MØDREGRUPPE FORÆLDRE"
replace mødregruppe = 1 if Gruppetype == "Mødregruppe"
replace mødregruppe = 1 if Gruppetype == "Mødregruppe - FORÆLDRE"
replace mødregruppe = 1 if Gruppetype == "Mødregruppe Nord 1. gang"
replace mødregruppe = 1 if Gruppetype == "Mødregruppe Nord Fleregang"
replace mødregruppe = 1 if Gruppetype == "Mødregruppe Syd 1. gang"
replace mødregruppe = 1 if Gruppetype == "Mødregruppe Syd Fleregang"
replace mødregruppe = 1 if Gruppetype == "Mødregruppe Vest 1. gang"
replace mødregruppe = 1 if Gruppetype == "Mødregruppe Vest Fleregang"
replace mødregruppe = 1 if Gruppetype == "Mødregruppe forældre"
replace mødregruppe = 1 if Gruppetype == "Mødregruppe mor"
replace mødregruppe = 1 if Gruppetype == "Mødregruppe(børnene)"
replace mødregruppe = 1 if Gruppetype == "Mødregruppe(voksne)"
replace mødregruppe = 1 if Gruppetype == "Mødregruppe. f"
replace mødregruppe = 1 if Gruppetype == "Mødreklub. ung mor"
replace mødregruppe = 1 if Gruppetype == "Ny forældregruppe"
replace mødregruppe = 1 if Gruppetype == "Opstart af mødregruppe"
replace mødregruppe = 1 if Gruppetype == "Præmatur mødregruppe"
replace mødregruppe = 1 if Gruppetype == "Ung mor mødregruppe"
replace mødregruppe = 1 if Gruppetype == "Ung mødregruppe"
replace mødregruppe = 1 if Gruppetype == "Unge mødre gruppe"
replace mødregruppe = 1 if Gruppetype == "mdr.gr/forældre"
replace mødregruppe = 1 if Gruppetype == "ny mødregruppe"
keep if mødregruppe == 1

//Create variable for date of group registration
gen date_gruppe = date(Indmeldsdato, "DMY")
format date_gruppe %td
drop Indmeldsdato
sort pnr date_gruppe 
duplicates report pnr date_gruppe GRUPPEID1 GRUPPEID2 // no duplicates by pnr, date and group
duplicates report pnr date_gruppe
order pnr cprm date_gruppe

drop Klassifikation AnsvarSHPL Modul Gruppetype
save "$work\grupperraw", replace

********************************************************************************
* Mothers have more than one entry/child -> split into dataset for each entry
* Find focal child for each entry -> closest dob to start date 
* Append together after locating focal children for each group X cprm
********************************************************************************

use "$work\grupperraw", clear
forvalues i=1/7 {
preserve
drop pnr
sort cprm date_gruppe GRUPPEID1
bys cprm: keep if _n==`i'
save "$work\gruppe_moms`i'.dta", replace
restore
}
forvalues i=1/7 {
use "$work\gruppe_moms`i'", clear
rename cprm id_mother
merge 1:m id_mother using "$work\mfr_kids", keep(3) nogen
format mfr_dob %td
gen diff=date_gruppe-mfr_dob
replace diff = . if diff < 0
bys id_mother: egen mindiff = min(diff)
gen relevantkid = .
replace relevantkid = 1 if mindiff == diff
keep if relevantkid == 1
save "$work\gruppe`i'", replace
}
use "$work\gruppe1", clear
forvalues i =2/7 {
append using "$work\gruppe`i'"
}
drop relevantkid mindiff

rename id_mother cprm
order pnr cprm date_gruppe mfr_yob mfr_dob Kommunekode
duplicates drop pnr date_gruppe GRUPPEID1, force
save "$work\grupper_matched", replace

********************************************************************************
* Some munics register children and not mother. Merge on mfr-kids
********************************************************************************
use "$work\grupperraw", clear
merge m:1 pnr using "$work\mfr_kids" , keep(3) nogen 
save "$work\grupper_matched_children", replace

********************************************************************************
* Append children-matched to mother-matched and drop duplicates
********************************************************************************
use "$work\grupper_matched", clear
append using "$work\grupper_matched_children"
duplicates drop pnr date_gruppe GRUPPEID1 GRUPPEID2, force
drop if diff > 365 
save "$work\grupper_matched_final", replace

********************************************************************************
* Cleaning
********************************************************************************
use "$work\grupper_matched_final", clear
gen year_gruppe = year(date_gruppe)

duplicates drop pnr date_gruppe, force
sort pnr date_gruppe
by pnr: keep if _n == 1 
 
drop GRUPPEID2 diff  mødregruppe
save "$work\grupper_wide", replace

********************************************************************************
* Coverage of NOVAX group data in the munic
* keep from the first year more than half of all births within muni is in group data
********************************************************************************
use "$work\grupper_wide", clear

// gen indicator for being in mother group data
gen ingrupper = 1

//merge with mfr_kids
merge 1:1 pnr using "$work\mfr_kids", nogen

replace ingrupper = 0 if ingrupper !=1
sort mfr_muni mfr_yob
collapse (mean) ingrupper, by(mfr_muni mfr_yob) 
gen treated = mfr_yob if ingrupper >=0.5
bysort mfr_muni: egen mintreat = min(treated)
gen years = 1 if mfr_yob>=mintreat
replace years=0 if mfr_yob < mintreat
keep if years == 1 

keep mfr_muni mfr_yob
save "$work\munics_grupper", replace

********************************************************************************
* New assignment (for robustness: are there moms who are assigned to new groups)
********************************************************************************
use "$work\grupper_matched_final", clear
keep pnr date_gruppe GRUPPEID1
sort pnr date_gruppe
by pnr date_gruppe: keep if _n == 1

bys pnr: gen no = _n
collapse no , by(pnr)
gen newassignment = 0
replace newassignment = 1 if no > 1 
drop no
save "$work\newassignment", replace
